//
//  EpointLoadMainConfigTask.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-4-1.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointLoadMainConfigTask.h"
#import "HttpUtil.h"
#import "GDataXMLNode.h"
#import "EpointMainConfigModel.h"
#import "EpointWebServiceUtil.h"

@implementation EpointLoadMainConfigTask

-(void)execute
{
    
    
    //修改模块配置项地址
    NSString *Epoint_MobileManager_URL = NSLocalizedString(@"Epoint_MobileManager_URL", @"");
    
//  NSString *url = [[NSUserDefaults standardUserDefaults]objectForKey:Epoint_User_MainConfig_URL];
    NSString *url = [Epoint_MobileManager_URL stringByReplacingOccurrencesOfString:@"services/MobileOaManage?wsdl" withString:@"mobileconfig/ios.xml"];
    NSString *bs = [HttpUtil getStr:url];
    
    if ([EpointLoadMainConfigTask getArray:bs].count>0) {
        NSUserDefaults *u = [NSUserDefaults standardUserDefaults];
        NSString *bsstore = [u objectForKey:Epoint_User_MainConfig];
        if (![bs isEqualToString:bsstore]) {
            //本地存储
            [u setObject:bs forKey:Epoint_User_MainConfig];
            [self sendDataRefreshUI:[EpointLoadMainConfigTask getPhoneArrByStore]];
        }
    }
    
}

+(NSMutableArray *)getPhoneArrByStore
{
    NSUserDefaults *u = [NSUserDefaults standardUserDefaults];
    NSString *bs = [u objectForKey:Epoint_User_MainConfig];
    NSMutableArray *arr = [EpointLoadMainConfigTask getArray:bs];
    if (CurrentDeviceISiPad) {
        return [EpointLoadMainConfigTask getPadMainIcon:arr];
    }
    return [EpointLoadMainConfigTask getPhoneMainIcon:arr];
}



+(NSMutableArray *)getExtendAppByStore
{
    NSUserDefaults *u = [NSUserDefaults standardUserDefaults];
    NSString *bs = [u objectForKey:Epoint_User_MainConfig];
    if (bs==nil) {
        return nil;
    }
    NSMutableArray *arr = [EpointLoadMainConfigTask getArray:bs];
    return [EpointLoadMainConfigTask getExtendIcon:arr];
}

+(NSMutableArray *)getExtendIcon:(NSMutableArray *)arr
{
    NSMutableArray *mainArr = [[NSMutableArray alloc]init];
    
    for (EpointMainConfigModel *m in arr) {
        if ([m.isDefault isEqualToString:@"2"]&&[m.type isEqualToString:@"3"]) {
            [mainArr addObject:m];
        }
    }
    return mainArr;
}

+(NSMutableArray *)getArray:(NSString *)bs
{
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:bs  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"module"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointMainConfigModel *m = [[EpointMainConfigModel alloc]init];
        
        GDataXMLElement *moduleID = [[arr elementsForName:@"moduleID"] objectAtIndex:0];
        m.moduleID = [moduleID stringValue];
        
        GDataXMLElement *type = [[arr elementsForName:@"type"] objectAtIndex:0];
        m.type = [type stringValue];
        
        GDataXMLElement *isDefault = [[arr elementsForName:@"isDefault"] objectAtIndex:0];
        m.isDefault = [isDefault stringValue];
        
        GDataXMLElement *modulename = [[arr elementsForName:@"modulename"] objectAtIndex:0];
        m.modulename = [modulename stringValue];
        
        GDataXMLElement *position = [[arr elementsForName:@"position"] objectAtIndex:0];
        m.position = [position stringValue];
        
        GDataXMLElement *platform = [[arr elementsForName:@"platform"] objectAtIndex:0];
        m.platform = [platform stringValue];
        
        GDataXMLElement *dyUrl = [[arr elementsForName:@"dyUrl"] objectAtIndex:0];
        m.dyUrl = [dyUrl stringValue];
        
        GDataXMLElement *paData = [[arr elementsForName:@"paData"] objectAtIndex:0];
        m.paData = [paData stringValue];
        
        GDataXMLElement *iconUrl = [[arr elementsForName:@"iconUrl"] objectAtIndex:0];
        m.iconUrl = [iconUrl stringValue];
        
        GDataXMLElement *apkUrl = [[arr elementsForName:@"apkUrl"] objectAtIndex:0];
        m.apkUrl = [apkUrl stringValue];
        
        GDataXMLElement *launchclass = [[arr elementsForName:@"launchclass"] objectAtIndex:0];
        m.launchclass = [launchclass stringValue];
        
        GDataXMLElement *packagename = [[arr elementsForName:@"packagename"] objectAtIndex:0];
        m.packagename = [packagename stringValue];
        
        GDataXMLElement *version = [[arr elementsForName:@"version"] objectAtIndex:0];
        m.version = [version stringValue];
        
        [listmp addObject:m];
    }
    return listmp;
}

+(NSMutableArray*)getPhoneMainIcon:(NSMutableArray *)arr
{
    NSMutableArray *mainArr = [[NSMutableArray alloc]init];
    
    for (EpointMainConfigModel *m in arr) {
        if ([m.isDefault isEqualToString:@"1"]&&([m.platform isEqualToString:@"1"]||[m.platform isEqualToString:@"2"])) {
            [mainArr addObject:m];
        }
    }
    return mainArr;
}

+(NSMutableArray*)getPadMainIcon:(NSMutableArray *)arr
{
    NSMutableArray *mainArr = [[NSMutableArray alloc]init];
    
    for (EpointMainConfigModel *m in arr) {
        if ([m.isDefault isEqualToString:@"1"]&&([m.platform isEqualToString:@"1"]||[m.platform isEqualToString:@"3"])) {
            [mainArr addObject:m];
        }
    }
    return mainArr;
}

@end
